home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / internet / linkpars / parser.bas < prev   
Encoding:
BASIC Source File  |  1995-08-27  |  16.0 KB  |  509 lines

  1. '============================================================================
  2. '                           VARIABLE DECLARATIONS
  3. '============================================================================
  4.  
  5. 'PLEASE SEE THE PROCEDURE TITLED "LICENSE" FOR DISCLAIMER AND OTHER
  6. 'PERTINENT INFO.
  7.  
  8.  
  9. Global FileName As String   'Stores name of html doc to open (you must write
  10.                 'the code that assigns a value to this variable.
  11.                 'This module will use the value to open the file
  12.                 'for parsing.
  13.  
  14. Global SrcCount As Long         'Stores number of links found by parse engine
  15. Global SrcLabel() As String     'Stores the caption of the hotlink
  16. Global SrcURL() As String       'Stores the URL of the hotlink
  17.  
  18. Global db As database           'Handle for HTML.mdb
  19. Global Dt As Dynaset            'Handle for TempLinks table
  20. Global Dp As Dynaset            'Handle for PermLinks table
  21. Global PDt As Dynaset           'Handle for Projects table
  22.  
  23. Global DisableDatabase          'Disables use of Access database in module.
  24.                 'Set value to TRUE if you experience
  25.                 'prolems with the database and wish to
  26.                 'use procedure without database storage.
  27.  
  28.  
  29. 'Pgm Constant for KeyDown
  30. Global Const KEY_DELETE = &H2E
  31.  
  32. 'Data Constants for Database use
  33. Global Const DB_LANG_GENERAL = ";LANGID=0x0809;CP=1252;COUNTRY=0"
  34. Global Const DB_LONG = 4
  35. Global Const DB_TEXT = 10
  36. Global Const DB_MEMO = 12
  37.  
  38.  
  39. Global ProjectTitle As String   'Stores name of Project
  40. Global DocTitle As String       'Stores Doc Title as found within <title></title>
  41.  
  42. Function BuildLink (Txt As String, URL As String) As String
  43.  
  44. BuildLink = "<a href=" + Chr$(34) + URL + Chr$(34) + ">" + Txt + "</a>"
  45.  
  46. End Function
  47.  
  48. Sub BuildLists (Order As Integer)
  49.  
  50. 'THIS ROUTINE BUILDS THE INITIAL LISTS OF DESCRIPTIONS AND URL'S.
  51. 'IT ALSO USES THE LOOP TO ADD THE RECORDS TO THE TEMPLINKS TABLE
  52. 'IN THE HTML.MDB DATABASE PROVIDING THE GLOBAL VAR DisableDatabse = False
  53.  
  54. 'The case argument tells the routine whether to sort the records in the order
  55. 'they were found or in alphabetical order.  This is only the case when using
  56. 'the database capabilities and will not work with DisableDatabase set to TRUE
  57.  
  58. Dim F As Form
  59. Dim L1 As Control
  60. Dim L2 As Control
  61. Dim Total As Integer, Count As Integer
  62.  
  63. Set F = Hotlist
  64. Set L1 = F.List1
  65. Set L2 = F.List2
  66.  
  67. L1.Clear
  68. L2.Clear
  69.  
  70. 'Does not add links to database if DisableDatabase = True
  71. If DisableDefault = True Then Order = 2
  72.  
  73. Select Case Order
  74.  
  75. 'Create list of references in default order as pulled from doc
  76. Case 0
  77.     Dim Ds1 As Dynaset
  78.     Dim Ds2 As Dynaset
  79.     Set Ds1 = db.CreateDynaset("Select Templinks.* From Templinks")
  80.     Ds1.MoveLast
  81.     Total = Ds1.RecordCount
  82.     Ds1.MoveFirst
  83.     Count = 0
  84.     Do Until Ds1.EOF
  85.     Count = Count + 1
  86.     F.Caption = "Creating database " + Str(Count) + " of" + Str(Total) + " links"
  87.     L1.AddItem Ds1("LinkText")
  88.     L1.ItemData(L1.NewIndex) = Ds1("LinkID")
  89.     L2.AddItem Ds1("LinkURL")
  90.     L2.ItemData(L2.NewIndex) = Ds1("LinkID")
  91.     Ds1.MoveNext
  92.     Loop
  93.  
  94. 'Create list of references in alphabetical order both by caption and URL
  95. Case 1
  96.     Dim Ds1 As Dynaset
  97.     Dim Ds2 As Dynaset
  98.     Set Ds1 = db.CreateDynaset("Select Templinks.* From Templinks Order by LinkText")
  99.     Set Ds2 = db.CreateDynaset("Select Templinks.* From Templinks Order by LinkURL")
  100.     Ds1.MoveLast
  101.     Total = Ds1.RecordCount
  102.     Ds1.MoveFirst
  103.     Ds2.MoveFirst
  104.     Count = 0
  105.     Do Until Ds1.EOF
  106.     Count = Count + 1
  107.     F.Caption = "Creating database " + Str(Count) + " of" + Str(Total) + " links"
  108.     L1.AddItem Ds1("LinkText")
  109.     L1.ItemData(L1.NewIndex) = Ds1("LinkID")
  110.     L2.AddItem Ds2("LinkURL")
  111.     L2.ItemData(L2.NewIndex) = Ds2("LinkID")
  112.     Ds1.MoveNext
  113.     Ds2.MoveNext
  114.     Loop
  115.  
  116. 'Avoid using database, Case 2 only used if DisableDatabase = TRUE
  117. Case 2
  118.     Total = SrcCount
  119.     Count = 0
  120.     For x = 0 To SrcCount - 1
  121.     Count = Count + 1
  122.     F.Caption = "Creating database " + Str(Count) + " of" + Str(Total) + " links"
  123.     L1.AddItem SrcLabel(x)
  124.     L1.ItemData(L1.NewIndex) = x
  125.     L2.AddItem SrcURL(x)
  126.     L2.ItemData(L2.NewIndex) = x
  127.     Next x
  128.  
  129. End Select
  130.  
  131. F.Caption = FileName
  132.  
  133. End Sub
  134.  
  135. Sub CheckDatabase ()
  136.  
  137. 'Check to see if database already exists or is disabled - if so exit sub
  138. If Dir$("html.mdb") <> "" Or DisableDatabase = True Then Exit Sub
  139.  
  140. 'Declare database object variables
  141. Dim NewDb As database
  142. Dim NewTd As New TableDef
  143. Dim F1 As New Field, F2 As New Field, F3 As New Field, F4 As New Field
  144.  
  145. 'Create database file HTML.mdb
  146. Set NewDb = CreateDatabase("html.mdb", DB_LANG_GENERAL)
  147.  
  148. 'Error handler
  149. If db Is Nothing Then MsgBox "Could not create database": Exit Sub
  150. If Err <> 0 Then MsgBox "Error during open:" & Err & "/" & Error$(Err)
  151. On Error GoTo 0
  152.  
  153. 'Define new table name
  154. NewTd.Name = "TempLinks"
  155.  
  156. 'Define new table fields
  157. F1.Name = "LinkID"
  158. F1.Type = DB_LONG
  159. NewTd.Fields.Append F1
  160.  
  161. F2.Name = "LinkText"
  162. F2.Type = DB_MEMO
  163. NewTd.Fields.Append F2
  164.  
  165. F3.Name = "LinkURL"
  166. F3.Type = DB_MEMO
  167. NewTd.Fields.Append F3
  168.  
  169. F4.Name = "URLType"
  170. F4.Type = DB_TEXT
  171. F4.Size = 10
  172. NewTd.Fields.Append F4
  173.  
  174. 'Create field def's in table def
  175. NewDb.TableDefs.Append NewTd
  176.  
  177. 'Msg user that database has been created
  178. MsgBox "html.mdb created"
  179.  
  180. 'Close the new database
  181. NewDb.Close
  182.  
  183. End Sub
  184.  
  185. Function CleanUpCaption (TempCap As String) As String
  186.  
  187. Dim Clean1 As Integer, Clean2 As Integer, Temp1 As String, Temp2 As String
  188. Dim Linktrim As Integer
  189.  
  190. 'FIND STYLE/IMG TAG DELIMITERS
  191. '-----------------------------
  192. Clean1 = InStr(TempCap, "<")
  193. Clean2 = InStr(TempCap, ">")
  194.  
  195. 'COPY TEXT NOT IN STYLE/IMG TAG
  196. '------------------------------
  197. If Clean1 <> 0 Then 'Or Clean1 <> "" Then
  198.     Temp1 = Left$(TempCap, Clean1 - 1)
  199. End If
  200.  
  201. If Clean2 <> 0 Then 'Or Clean2 <> "" Then
  202.     Temp2 = Trim$(Right$(TempCap, Len(TempCap) - Clean2))
  203. End If
  204.  
  205. 'COPY REMAINING TEXT BACK TO CAPTION VARIABLE
  206. '--------------------------------------------
  207. If Clean1 > 0 And Clean2 > 0 Then
  208.     TempCap = Temp1 + Temp2
  209. End If
  210.  
  211. 'EXCHANGE CR AND LF FOR SPACES
  212. '-----------------------------
  213. For Linktrim = 1 To Len(TempCap)
  214.     If Asc(Mid$(TempCap, Linktrim, 1)) = 10 Or Asc(Mid$(TempCap, Linktrim, 1)) = 13 Then
  215.     Mid$(TempCap, Linktrim, 1) = " "
  216.     End If
  217. Next Linktrim
  218.  
  219. 'RETURN CLEANED UP CAPTION TO PARSE ROUTINE
  220. '------------------------------------------
  221. CleanUpCaption = TempCap
  222.  
  223. End Function
  224.  
  225. Sub LICENSE ()
  226. '===========================================================================
  227. '                         HTML HOTLINK PARSE ROUTINE
  228. '===========================================================================
  229. 'DISCLAIMER
  230. '----------
  231. '
  232. 'This module is made freely available to the public and may be reproduced,
  233. 'in whole or in part, as well as redistributed without royalty.  Use of the
  234. 'routines in this module are the sole responsibility of you, the user.  I
  235. 'make no warranty, written or implied, regarding any portion of this module.
  236. 'It has run on my US3486/33 board with 20 megs RAM without incident.  Before
  237. 'using this module with any critical data please read through all the
  238. 'functions and procedures and make sure you understand what they're doing.
  239. 'There are always potential problems when disk I/O is involved.
  240. '
  241. 'This module utilizes form, componant, and database object variables.  Use of
  242. 'these are only available in the PRO edition of VB.  Please contact Microsoft
  243. 'or your local software vendor for more information on updating to VB-Pro.
  244. '
  245. 'This routine will create a Microsoft Access Database the first time you use
  246. 'it.  I am currently using Microsoft Access 2.0 with the compatability layer
  247. 'installed.  If you experience problems with the database portion of the
  248. 'module I recommend you:
  249. '
  250.     'a) install the compatability layer on your system.
  251.     '   This can be found using FTP to "microsoft.com".
  252. '
  253.     'b) disable the use of the database by changing the
  254.     '   "DisableDatabase" global variable from False to
  255.     '   True.
  256. '
  257. 'If DisableDatabase is set to true then you will need to comment out the
  258. 'following Global Declarations from the General Declarations section of
  259. 'this module:
  260. '
  261. 'Global db As database           'Handle for HTML.mdb
  262. 'Global Dt As Dynaset            'Handle for TempLinks table
  263. 'Global Dp As Dynaset            'Handle for PermLinks table
  264. 'Global PDt As Dynaset           'Handle for Projects table
  265. '
  266. 'This module makes judicious use of form and componant object variables so
  267. 'using it should be a simple matter of changing the form and control names
  268. 'in the appropriate declarations.
  269. '
  270. '
  271.  
  272. End Sub
  273.  
  274. Sub ParseLinks (FileName As String)
  275.  
  276. Dim FileNum As Integer  'Number of next available free filenumber
  277. Dim LinkText As String   'Contents of HTML document opened
  278. Dim TempVar As String   'Temporary container for document labels
  279.  
  280. Dim LinkStart As Long        'Holds starting position of element to be parsed
  281. Dim LinkEnd As Long          'Holds ending position of element to be parsed
  282.  
  283. Dim FileSize As Long        'Used for status line (percentage completed)
  284.  
  285. Dim Lycos As Integer            'Flags true if user answers yes on lycos search
  286. Dim NotLycos As Integer         'Flags for second try on lycos query
  287.  
  288. '----------------------------------------------------------------------------
  289.  
  290. 'Check for existance of "html.mdb".  If it does not exist routine will create it.
  291. If DisableDatabase = False Then CheckDatabase
  292.  
  293. 'If using database
  294. If DisableDatabase = False Then
  295.     Set Dt = db.CreateDynaset("TempLinks")  'Global Var declared in General Declarations.
  296.     db.Execute "Delete From Templinks"      'Delete any pre-existing records in temp database
  297. End If
  298.  
  299. '============================================================================
  300. '                         Open HTML file for parsing
  301. '============================================================================
  302. 'FIND FIRST AVAILABLE FILE NUMBER
  303. '--------------------------------
  304. FileNum = FreeFile
  305.  
  306. 'OPEN FILE WITH FILE NUMBER
  307. '--------------------------
  308. Open FileName For Input As FileNum  'ATT: You must write code to assign value to Filename
  309.  
  310. 'ASSIGN FILE CONTENTS TO VARIABLE
  311. '--------------------------------
  312. LinkText = Input$(LOF(FileNum), FileNum)
  313. FileSize = LOF(FileNum)
  314. '============================================================================
  315. '                             Get document Title
  316. '============================================================================
  317. LinkStart = InStr(UCase(LinkText), "<TITLE>")
  318. LinkStart = LinkStart + Len("<TITLE>")
  319. LinkEnd = InStr(UCase(LinkText), "</TITLE>")
  320.  
  321. DocTitle = Mid$(LinkText, LinkStart, LinkEnd - LinkStart)
  322.  
  323. '============================================================================
  324. '                          Begin Parsing Document
  325. '============================================================================
  326. 'PROGRAM LABEL
  327. '-------------
  328. ParseLinksStart:
  329.  
  330. URLRecognized = False
  331.  
  332. 'ERROR CONTROL
  333. '-------------
  334. On Error Resume Next
  335.  
  336. 'FIND NEXT HOTLINK REFERENCE
  337. '---------------------------
  338. LinkStart = InStr(1, UCase(LinkText), "HREF")
  339.  
  340. '*** CHECK FOR NO MORE HOTLINKS LEFT AND IF TRUE THEN ESCAPE FROM ROUTINE ***
  341. '----------------------------------------------------------------------------
  342. If LinkStart = 0 Then 'Or LinkStart = "" Then
  343.     Exit Sub
  344. End If
  345.  
  346. 'INFORM USER OF PERCENTAGE COMPLETED
  347. '-----------------------------------
  348. Hotlist.Caption = "Scanning File: " + Format((Len(LinkText) / FileSize), "0%")
  349.  
  350. 'TRUNCATE TEXT TO BEGINING OF HREF
  351. '---------------------------------
  352. LinkText = Mid$(LinkText, LinkStart, Len(LinkText) - LinkStart)
  353.  
  354. 'FIND POSITION OF FIRST URL DELIMTER
  355. '------------------------------------
  356. LinkStart = InStr(1, LinkText, Chr$(34))
  357.  
  358. 'TRUNCATE TEXT UP TO AND INCLUDING FIRST URL DELIMITER
  359. '-----------------------------------------------------
  360. LinkText = Mid$(LinkText, LinkStart + 1, Len(LinkText) - LinkStart)
  361.  
  362. 'FIND POSITION OF SECOND URL DELIMITER
  363. '-------------------------------------
  364. LinkEnd = InStr(1, LinkText, Chr$(34))
  365.  
  366. 'EXTRACT URL AND ASSIGN TO LOCAL VAR
  367. '-----------------------------------
  368. LinkURL = Mid$(LinkText, 1, LinkEnd - 1)
  369.  
  370. 'TRUNCATE TEXT UP TO AND INCLUDING SECOND URL DELIMITER
  371. '------------------------------------------------------
  372. LinkText = Mid$(LinkText, LinkEnd + 1, Len(LinkText) - LinkEnd)
  373.  
  374. 'FIND CAPTION DELIMITER
  375. '----------------------
  376. LinkStart = InStr(1, LinkText, ">")
  377.  
  378. 'TRUNCATE TEXT UP TO AND INCLUDING FIRST CAPTION DELIMITER
  379. '---------------------------------------------------------
  380. LinkText = Mid$(LinkText, LinkStart + 1, Len(LinkText) - LinkStart)
  381.  
  382. 'FIND SECOND CAPTION DELIMITER
  383. '-----------------------------
  384. LinkEnd = InStr(1, LinkText, "</")
  385.  
  386. 'EXTRACT CAPTION AND ASSIGN TO LOCAL VAR
  387. '---------------------------------------
  388. LinkCaption = Mid$(LinkText, 1, LinkEnd - 1)
  389.  
  390. 'CLEAN UP VARIOUS FORMATTING ITEMS EMBEDDED IN LINK CAPTION
  391. 'AND ASSIGN RESULT TO BACK TO LINK CAPTION VARIABLE
  392. '----------------------------------------------------------
  393. TempVar = LinkCaption
  394. LinkCaption = CleanUpCaption(TempVar)
  395. LinkCaption = TempVar
  396.  
  397. 'REMOVE EXCESS WHITE SPACE FROM LOCAL VARS
  398. '-----------------------------------------
  399. LinkCaption = Trim$(LinkCaption)
  400. LinkURL = Trim$(LinkURL)
  401.  
  402. 'IF NOT LINK CAPTION ASSIGN DEFAULT CAPTION
  403. '------------------------------------------
  404. If LinkCaption = "" Then LinkCaption = LinkURL
  405.  
  406. '============================================================================
  407. '                          Check for Lycos Search
  408. '============================================================================
  409. 'NotLycos = TRUE IF USER RESPONDS TO NEXT LINE THAT HTML DOC IS A LYCOS
  410. 'SEARCH.  IF IT IS A LYCOS SEARCH THIS LINE CAUSES THE EXECUTION TO SKIP
  411. 'THE OTHER TWO CODE SECTIONS.
  412. '-----------------------------------------------------------------------
  413. If NotLycos = True Then GoTo ParseLinksNextSegment
  414.  
  415. 'ASK USER IF HTML DOC IS RESULTS OF LYCOS SEARCH
  416. 'IF TRUE THEN NotLycos IS SET TO TRUE
  417. '-----------------------------------------------
  418. If Lycos = False And InStr(UCase(LinkURL), "LYCOS") <> 0 Then
  419.     If MsgBox("There are indications that the document you submited was the result of a Lycos Search.  Is this correct?", 4, "Confirm Document Origin") = 6 Then
  420.     Lycos = True
  421.     Else
  422.     NotLycos = True
  423.     End If
  424. End If
  425.  
  426. 'IF LYCOS IS FOUND IN SUBSEQUENTLY PARSED URL'S THEN THE PARSE
  427. 'ROUTINE REPEATS ITSELF WITH THE NEXT LINK, FAILING TO ADD THE
  428. 'LYCOS LINK TO THE ARRAY
  429. '-------------------------------------------------------------
  430. If Lycos = True And InStr(UCase(LinkURL), "LYCOS") <> 0 Then
  431.     GoTo ParseLinksStart
  432. End If
  433.  
  434. '============================================================================
  435. '                       Build Hotlink Variable Arrays
  436. '============================================================================
  437. ParseLinksNextSegment:
  438.  
  439. 'ADD DESCRIPTION TO PROPER LIST
  440. '------------------------------
  441. ReDim Preserve SrcLabel(SrcCount)
  442. ReDim Preserve SrcURL(SrcCount)
  443.  
  444. SrcLabel(SrcCount) = LinkCaption
  445. SrcURL(SrcCount) = LinkURL
  446.  
  447. 'IF DATABASE IS ENABLED THEN
  448. 'ADD RECORD TO TEMP TABLE OF DATABASE
  449. '------------------------------------
  450. If DisableDatabase = False Then
  451.     Dt.AddNew
  452.     Dt("LinkID") = SrcCount
  453.     Dt("LinkText") = SrcLabel(SrcCount)
  454.     Dt("LinkURL") = SrcURL(SrcCount)
  455.     Dt("URLType") = URLType(SrcURL(SrcCount))   '<-- This is a User Function
  456.     Dt.Update
  457. End If
  458.  
  459. 'INCREMENT SrcCount VARIABLE
  460. '---------------------------
  461. SrcCount = SrcCount + 1
  462.  
  463. 'RETURN TO BEGINING OF ROUTINE
  464. '-----------------------------
  465. GoTo ParseLinksStart
  466.  
  467.  
  468. End Sub
  469.  
  470. Function URLType (URL As String) As String
  471.  
  472. Select Case UCase(Left$(URL, 7))
  473. Case "HTTP://"
  474.     URLType = "WWW"
  475.     Exit Function
  476.     
  477. Case "TELNET:"
  478.     URLType = "TELNET"
  479.     Exit Function
  480.  
  481. Case "GOPHER:"
  482.     URLType = "GOPHER"
  483.     Exit Function
  484.  
  485. Case "MAILTO:"
  486.     URLType = "MAILTO"
  487.     Exit Function
  488.  
  489. Case "FILE://"
  490.     URLType = "FILE"
  491.     Exit Function
  492.  
  493. End Select
  494.  
  495. If UCase(Left$(URL, 6)) = "FTP://" Then
  496.     URLType = "FTP"
  497.     Exit Function
  498. End If
  499.  
  500. If UCase(Left$(URL, 5)) = "NEWS:" Then
  501.     URLType = "NEWS"
  502.     Exit Function
  503. End If
  504.  
  505. URLType = "FILE"
  506.  
  507. End Function
  508.  
  509.